package com.orhon.smartcampus.modules.student.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.ArrivesDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class ArriveFetcher implements DataFetcher {
    @Autowired
    ArrivesDao arrivesDao;

    @Autowired
    TokenService tokenService;


    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Arrives where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        Arrives item = (Arrives) arrivesDao.findId(hql, null);
        if(item!=null) {
        	GQLGrades gqlGrades = null;
            if (env.getSelectionSet().contains("grade")){
                item.getBaseGradesByGradeId().toString();
                Grades grades = item.getBaseGradesByGradeId();
                 gqlGrades = GradeMapper.MAPPER.toGrade(grades);
            }

            GQLPeriod gqlPeriod = null;
            if (env.getSelectionSet().contains("period")){
                item.getBasePeriodsByPeriodId().toString();
                Periods periods = item.getBasePeriodsByPeriodId();
                gqlPeriod = PeriodMapper.MAPPER.toPeriods(periods);
            }
            
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                item.getBaseSchoolsBySchoolId().toString();
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            
            GQLArrives arrives = ArrivesMapper.MAPPER.toArrives(item);
            arrives.setGrade(gqlGrades);
            arrives.setPeriod(gqlPeriod);
            arrives.setSchool(gqlSchool);
            return arrives;
        }
		return null;
        
    }


}
